package floappcontest.core.backbeans;


import java.util.List;

import floappcontest.core.beans.Component;
import floappcontest.core.control.Control;
import floappcontest.core.dao.factory.HibernateDAOFactory;
import floappcontest.core.exception.FloAppException;

public class ComponentBean {
	
	private static final String FAILED = "FAILED"; //$NON-NLS-1$

	private static final String OK = "OK"; //$NON-NLS-1$

	private Component component;

	private Control control;
	
	private List<Component> list;
	
	public ComponentBean() {
		setControl( new Control(new HibernateDAOFactory()) );
		setComponent( new Component() );
		refreshList();
	}
		
	public Component getComponent() {
		return this.component;
	}

	public void setComponent(Component component) {
		this.component = component;
	}

	public List<Component> list(){
		return null;
	}
	
	public String insertComponent(){
		try {
			getControl().insertComponent( getComponent() );
			setComponent( new Component() );
			return OK;
		} catch ( FloAppException e ) {
			e.printStackTrace();
			setComponent( new Component() );
			return FAILED;
		}finally{
			refreshList();
		}
	}
	
	/**
	 * @return
	 */
	public Control getControl() {
		return this.control;
	}

	/**
	 * @param control
	 */
	public void setControl(Control control) {
		this.control = control;
	}
	
	public List<Component> getList() {
		return this.list;
	}

	public void setList(List<Component> list) {
		this.list = list;
	}
	
	public void refreshList(){
		setList( getControl().listComponents() );
	}
	
	public String removeComponent(){
		getControl().removeComponent( getComponent().getName() );
		refreshList();
		return new MenuBarRedirection().listComponentPage();
	}


}
